﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using BlobTowerDefense.Tools;
using BlobTowerDefense.Resources;

namespace BlobTowerDefense
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        public SettingsPage()
        {
            InitializeComponent();
            Control_Languages.Language_Changed += new TextChangedEventHandler(Control_Languages_Language_Changed);
        }

        void Control_Languages_Language_Changed(object sender, TextChangedEventArgs e)
        {
            this.ApplicationTitle.Text = ApplicationResources.Get("AppTitle");
            this.PageTitle.Text = ApplicationResources.Get("PageTitle_Settings");

            this.Control_Controls.TextBlock_LeftHanded.Text = ApplicationResources.Get("Settings_Label_Lefthanded");
            this.Control_Controls.TextBlock_Sensitivity.Text = ApplicationResources.Get("Settings_Label_Sensitivity");
            this.Control_Controls.TextBlock_Nickname.Text = ApplicationResources.Get("Settings_Nickname_Label");

            this.Control_Audio.TextBlock_VolumeMaster_Label.Text = ApplicationResources.Get("Settings_Label_VolumeMaster");
            this.Control_Audio.TextBlock_VolumeMusic_Label.Text = ApplicationResources.Get("Settings_Label_VolumeMusic");
            this.Control_Audio.TextBlock_VolumeSounds_Label.Text = ApplicationResources.Get("Settings_Label_VolumeSounds");
        }

        private void ButtonControls_Click(object sender, RoutedEventArgs e)
        {
            if (Control_Audio.Visibility == System.Windows.Visibility.Visible)
            {
                Control_Audio.animationOut_LeftToRight.Completed += new EventHandler(ControlControls_animationOut_LeftToRight_Completed);
                Control_Audio.animationOut_LeftToRight.Begin();
            }
            else if (Control_Languages.Visibility == System.Windows.Visibility.Visible)
            {
                Control_Languages.animationOut_LeftToRight.Completed += new EventHandler(ControlControls_animationOut_LeftToRight_Completed);
                Control_Languages.animationOut_LeftToRight.Begin();
            }           
        }

        void ControlControls_animationOut_LeftToRight_Completed(object sender, EventArgs e)
        {
            Control_Controls.Visibility = System.Windows.Visibility.Visible;
            Control_Audio.Visibility = System.Windows.Visibility.Collapsed;
            Control_Languages.Visibility = System.Windows.Visibility.Collapsed;

            Control_Controls.animationIn_LeftToRight.Begin();
        }

        void ControlControls_animationOut_RightToLeft_Completed(object sender, EventArgs e)
        {
            Control_Controls.Visibility = System.Windows.Visibility.Visible;
            Control_Audio.Visibility = System.Windows.Visibility.Collapsed;
            Control_Languages.Visibility = System.Windows.Visibility.Collapsed;

            Control_Controls.animationIn_RightToLeft.Begin();
        }

        private void ButtonAudio_Click(object sender, RoutedEventArgs e)
        {
            if (Control_Controls.Visibility == System.Windows.Visibility.Visible)
            {
                Control_Controls.animationOut_RightToLeft.Completed += new EventHandler(ControlAudio_animationOut_RightToLeft_Completed);
                Control_Controls.animationOut_RightToLeft.Begin();
            }

            else if (Control_Languages.Visibility == System.Windows.Visibility.Visible)
            {
                Control_Languages.animationOut_LeftToRight.Completed += new EventHandler(ControlAudio_animationOut_LeftToRight_Completed);
                Control_Languages.animationOut_LeftToRight.Begin();
            }
        }

        void ControlAudio_animationOut_LeftToRight_Completed(object sender, EventArgs e)
        {
            Control_Controls.Visibility = System.Windows.Visibility.Collapsed;
            Control_Audio.Visibility = System.Windows.Visibility.Visible;
            Control_Languages.Visibility = System.Windows.Visibility.Collapsed;

            Control_Audio.animationIn_LeftToRight.Begin();
        }

        void ControlAudio_animationOut_RightToLeft_Completed(object sender, EventArgs e)
        {
            Control_Controls.Visibility = System.Windows.Visibility.Collapsed;
            Control_Audio.Visibility = System.Windows.Visibility.Visible;
            Control_Languages.Visibility = System.Windows.Visibility.Collapsed;

            Control_Audio.animationIn_RightToLeft.Begin();
        }

        private void ButtonLanguage_Click(object sender, RoutedEventArgs e)
        {
            if (Control_Controls.Visibility == System.Windows.Visibility.Visible)
            {
                Control_Controls.animationOut_RightToLeft.Completed += new EventHandler(ControlLanguages_animationOut_RightToLeft_Completed);
                Control_Controls.animationOut_RightToLeft.Begin();
            }
            else if (Control_Audio.Visibility == System.Windows.Visibility.Visible)
            {
                Control_Audio.animationOut_RightToLeft.Completed += new EventHandler(ControlLanguages_animationOut_RightToLeft_Completed);
                Control_Audio.animationOut_RightToLeft.Begin();
            }
        }

        void ControlLanguages_animationOut_LeftToRight_Completed(object sender, EventArgs e)
        {
            Control_Controls.Visibility = System.Windows.Visibility.Collapsed;
            Control_Audio.Visibility = System.Windows.Visibility.Collapsed;
            Control_Languages.Visibility = System.Windows.Visibility.Visible;

            Control_Languages.animationIn_LeftToRight.Begin();
        }

        void ControlLanguages_animationOut_RightToLeft_Completed(object sender, EventArgs e)
        {
            Control_Controls.Visibility = System.Windows.Visibility.Collapsed;
            Control_Audio.Visibility = System.Windows.Visibility.Collapsed;
            Control_Languages.Visibility = System.Windows.Visibility.Visible;

            Control_Languages.animationIn_RightToLeft.Begin();
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Landscape) == (PageOrientation.Landscape))
            {
                if (Loader.Load_LeftHanded())
                {
                    Grid_ContentPanel.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Auto);
                    Grid_ContentPanel.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);

                    Grid.SetColumn(Control_Controls, 1);
                    Grid.SetColumn(Control_Audio, 1);
                    Grid.SetColumn(Control_Languages, 1);

                    Grid.SetRow(Grid_Settings, 0);
                    Grid.SetColumn(Grid_Settings, 0);
                    Grid.SetColumnSpan(Grid_ContentPanel, 1);
                }
                else
                {
                    Grid_ContentPanel.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
                    Grid_ContentPanel.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Auto);

                    Grid.SetColumn(Control_Controls, 0);
                    Grid.SetColumn(Control_Audio, 0);
                    Grid.SetColumn(Control_Languages, 0);

                    Grid.SetRow(Grid_Settings, 0);
                    Grid.SetColumn(Grid_Settings, 1);
                    Grid.SetColumnSpan(Grid_ContentPanel, 1);
                }
            }
            else
            {
                Grid_ContentPanel.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
                Grid_ContentPanel.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Auto);

                Grid.SetColumn(Control_Controls, 0);
                Grid.SetColumn(Control_Audio, 0);
                Grid.SetColumn(Control_Languages, 0);

                Grid.SetRow(Grid_Settings, 1);
                Grid.SetColumn(Grid_Settings, 0);
                Grid.SetColumnSpan(Grid_ContentPanel, 2);
            }

            BitmapImage imageBackground;
            if ((e.Orientation & PageOrientation.Landscape) == (PageOrientation.Landscape))
            {
                imageBackground = new BitmapImage(new Uri("../Images/Backgrounds/Background_Settings_Landscape.png", UriKind.Relative));
            }
            else
            {
                imageBackground = new BitmapImage(new Uri("../Images/Backgrounds/Background_Settings_Portrait.png", UriKind.Relative));
            }

            if (imageBackground != null)
            {
                ((ImageBrush)LayoutRoot.Background).ImageSource = imageBackground;
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            animation_Menu_Hide_FromUp.Completed += new EventHandler(animation_Menu_Hide_FromUp_Completed);
            animation_Menu_Hide_FromUp.Begin();
            e.Cancel = true;
        }

        void animation_Menu_Hide_FromUp_Completed(object sender, EventArgs e)
        {
            animation_Menu_Hide_FromUp.Completed -= new EventHandler(animation_Menu_Hide_FromUp_Completed);
            NavigationService.Navigate(new Uri("/Pages/MainPage.xaml", UriKind.Relative));
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            animation_Menu_Show_FromDown.Begin();
            base.OnNavigatedTo(e);
        }
    }
}